package com.gantzgulch.generics;

import static com.gantzgulch.generics.Cast.cast;

import java.lang.reflect.Array;

public class StaticFactory {

    public static <T> T createObject(Class<T> myClass){
        try {
            return myClass.newInstance();
        } catch (Exception e) {
        	throw Objects.ensureRuntimeException(e);
        }
    }

    public static <T> T[] createArray(Class<T> myClass, int length){
        return cast(Array.newInstance(myClass, length));
    }
    
    public static <T> T[] createArrayWithDefaults(Class<T> myClass, int length){
        
        T[] array = createArray(myClass, length);

        for(int i=0; i<array.length; i++){
        	array[i] = createObject(myClass);
        }
        
        return array;
    }
}
